package com.tjvib.presenter;

import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.IBinder;

import com.tjvib.bean.SensorInfo;
import com.tjvib.bean.SensorSelect;
import com.tjvib.common.Callback;
import com.tjvib.sensor.LpmsB2SensorData;
import com.tjvib.service.LpmsB2SensorService;
import com.tjvib.util.ToastUtil;
import com.tjvib.view.activity.LpmsB2SensorActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;

public class LpmsB2SensorPresenter extends SensorPresenter<LpmsB2SensorService, LpmsB2SensorActivity> {

    private static final String TAG = "LpmsB2SensorPresenter";

    SensorInfo curSensorInfo;
    List<SensorInfo> sensorInfoList;

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            LpmsB2SensorService.LocalBinder binder = (LpmsB2SensorService.LocalBinder) service;
            mService = (LpmsB2SensorService) binder.getService();
            bound = true;
            sensorInfoList = mService.getConnectedLpmsB2SensorInfoList();
            if (sensorInfoList.isEmpty()) {
                view.showErrorDialog("当前没有设备连接");
                return;
            }
            curSensorInfo = sensorInfoList.get(0);
            view.refreshSensorSelectList();
            startReadingData();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            bound = false;
        }
    };

    private TimerTask readDataTask = new TimerTask() {
        @Override
        public void run() {
            if (bound) {
                LpmsB2SensorData data = mService.getData(curSensorInfo.getAddress());
                if (data == null)
                    return;
                view.setData(data);
            }
        }
    };

    public LpmsB2SensorPresenter(LpmsB2SensorActivity view) {
        super(view);
    }

    @Override
    public ServiceConnection getServiceConnection() {
        return mConnection;
    }

    @Override
    public Class<?> getServiceClass() {
        return LpmsB2SensorService.class;
    }

    @Override
    public TimerTask getReadDataTask() {
        return readDataTask;
    }

    @Override
    public void setStreamFreq(int freq) {
        mService.setStreamFrequency(curSensorInfo.getAddress(), freq);
    }

    @Override
    public int getStreamFreq() {
        return mService.getStreamFrequency(curSensorInfo.getAddress());
    }

    @Override
    public void discardData() {
        if (mService != null)
            mService.discardData();
        else
            ToastUtil.show("服务未启动");
    }

    @Override
    public void storeData(String dataName, Callback callback) {
        if (mService != null)
            mService.storeData(dataName, callback);
        else
            ToastUtil.show("服务未启动");
    }

    @Override
    public void resetTimestamp() {
        if (mService != null)
            mService.resetTimestamp(curSensorInfo.getAddress());
        else
            ToastUtil.show("服务未启动");
    }

    @Override
    public String getDataSetType() {
        return "lpmsb2";
    }

    @Override
    String getTag() {
        return TAG;
    }

    public List<SensorSelect> getSensorSelectList() {
        List<SensorSelect> result = new ArrayList<>();
        for (SensorInfo info : sensorInfoList) {
            result.add(new SensorSelect(info.getName(), info.getAddress(), info == curSensorInfo));
        }
        return result;
    }

    public void switchSensorInfo(int position) {
        curSensorInfo = sensorInfoList.get(position);
        view.clearSeries();
    }

    public String getCurName() {
        return curSensorInfo.getName();
    }

    public int getImuId() {
        return mService.getImuId(curSensorInfo.getAddress());
    }

    public void setImuId(int value) {
        mService.setImuId(curSensorInfo.getAddress(), value);
    }

    public int getGyroRange() {
        return mService.getGyroRange(curSensorInfo.getAddress());
    }

    public void setGyroRange(int value) {
        mService.setGyroRange(curSensorInfo.getAddress(), value);
    }

    public int getAccRange() {
        return mService.getAccRange(curSensorInfo.getAddress());
    }

    public void setAccRange(int value) {
        mService.setAccRange(curSensorInfo.getAddress(), value);
    }

    public int getMagRange() {
        return mService.getMagRange(curSensorInfo.getAddress());
    }

    public void setMagRange(int value) {
        mService.setMagRange(curSensorInfo.getAddress(), value);
    }

    public boolean isAccDataEnabled() {
        return mService.isAccDataEnabled(curSensorInfo.getAddress());
    }

    public boolean isGyroDataEnabled() {
        return mService.isGyroDataEnabled(curSensorInfo.getAddress());
    }

    public boolean isMagDataEnabled() {
        return mService.isMagDataEnabled(curSensorInfo.getAddress());
    }

    public boolean isAngularVelDataEnable() {
        return mService.isAngularVelDataEnable(curSensorInfo.getAddress());
    }

    public boolean isQuaternionDataEnabled() {
        return mService.isQuaternionDataEnabled(curSensorInfo.getAddress());
    }

    public boolean isEulerDataEnabled() {
        return mService.isEulerDataEnabled(curSensorInfo.getAddress());
    }

    public boolean isLinAccDataEnabled() {
        return mService.isLinAccDataEnabled(curSensorInfo.getAddress());
    }

    public boolean isPressureDataEnabled() {
        return mService.isPressureDataEnabled(curSensorInfo.getAddress());
    }

    public boolean isAltitudeDataEnabled() {
        return mService.isAltitudeDataEnabled(curSensorInfo.getAddress());
    }

    public boolean isTemperatureDataEnabled() {
        return mService.isTemperatureDataEnabled(curSensorInfo.getAddress());
    }

    public void setTransmissionData(int value) {
        mService.setTransmissionData(curSensorInfo.getAddress(), value);
    }

    public int getFilterMode() {
        return mService.getFilterMode(curSensorInfo.getAddress());
    }

    public void setFilterMode(int value) {
        mService.setFilterMode(curSensorInfo.getAddress(), value);
    }

    public void setOrientationOffset(int value) {
        mService.setOrientationOffset(curSensorInfo.getAddress(), value);
    }

    public void resetOrientationOffset() {
        mService.resetOrientationOffset(curSensorInfo.getAddress());
    }
}
